/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mvc.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;


/**
 *
 * @author MAN
 */
public class Vigilante_Dao extends Model implements DaoGenerico {


     public boolean existe(Object o) throws SQLException{
             Vigilante per=(Vigilante)o;
             Connection conex=getCon();
		boolean sw=true;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT COUNT(*) FROM vigilantes WHERE cedula=? ");
			pstmt.setString(1, per.getCedula());
                        
			rs=pstmt.executeQuery();
			if(rs.next())
				if(rs.getString(1).equals("0"))
					sw=false;
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return sw;
	}



      public Object load(ResultSet rs) throws SQLException {

        Vigilante u = new Vigilante();
        u.setCodigo(rs.getString(1));
        u.setCedula(rs.getString(2));
        u.setNombre(rs.getString(3));
        u.setSegundo_nombre(rs.getString(4));
        u.setApellido(rs.getString(5));
        u.setSegundo_pellido(rs.getString(6));
        
        //u.setDireccion(rs.getString(7));
        u.setE_mail(rs.getString(8));
        u.setFecha_naci(rs.getString(9));


        return u;
    }


      public Object extrae (Object id) throws SQLException{
          String id1=(String)id;
                    Connection conex=getCon();

                if (!isConnect())
			throw new SQLException("no hay conexcion");
                Vigilante v= new Vigilante();
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT * FROM vigilantes WHERE codigo=? ");
			pstmt.setString(1,id1);
			rs=pstmt.executeQuery();
			if(rs.next()){
			  v=(Vigilante)load(rs);

                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return v;
     }


        public void crear(Object o)throws SQLException{
         Vigilante v=(Vigilante)o;
             Connection conex=getCon();

                if(existe(v))
                    throw new SQLException("Ya existe este vigilante ");
		PreparedStatement pstmt=null;

		try{

			pstmt=conex.prepareStatement("INSERT INTO vigilantes VALUES (?,?,?,?,?,?,?,?,?)");


			pstmt.setString(1,v.getCodigo());
			pstmt.setString(2,v.getNombre());
			pstmt.setString(3,v.getSegundo_nombre());
                        pstmt.setString(4,v.getApellido());
                        pstmt.setString(5,v.getSegundo_pellido());
                        pstmt.setString(6,v.getCedula());
                        pstmt.setInt(7, v.getDireccion().getId());
                        pstmt.setString(8,v.getE_mail());
                        pstmt.setString(9,v.getFecha_naci());


			pstmt.executeUpdate();

		}finally{
			if(pstmt!=null){
				pstmt.close();
				pstmt=null;
			}
		}

  }



       public List listar() throws SQLException {
              Connection conex=getCon();
        if (!isConnect())
		throw new SQLException("no hay conexcion");

        PreparedStatement ps = null;
        ResultSet rs = null;
        Vigilante u = null;
        List lista = new LinkedList();
        try {
            ps =(PreparedStatement)  conex.prepareStatement("select * from vigilantes ");
            rs = ps.executeQuery();

            while (rs.next()) {
                u =(Vigilante) load(rs);
                lista.add(u);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

       

    public Object extrae2(Object id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List listar(Object o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Update(Object o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

      






}
